home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / polyIcosahedron.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  4.8 KB  |  185 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. // Description : Construct an icosahedron given the length of the edge.
  19. //
  20.  
  21. proc int remainder( int $x, int $y )
  22. //
  23. //
  24. {
  25.     int $q = $x/$y ;
  26.     return $x - $q*$y   ;
  27. }
  28. // Find the radius of circle encompasing the polygon. 
  29. //
  30. proc float boundingCircleRadius( int $n, float $x )
  31. //
  32. //    Description:
  33. //        number of sides on the polygon.
  34. //        $x = length of the polygon.
  35. //
  36. {
  37.     float $rad ;
  38.     float $angle = 360.0 / $n ;
  39.  
  40.     float $cosx = cos(deg_to_rad($angle)) ;     
  41.     float $den = 2.0 * ( 1.0 - $cosx ) ;
  42.     $rad = $x / sqrt($den) ;
  43.     return $rad ;
  44. }
  45.  
  46. proc float[] createRegularPolygonX( int $n, float $r )
  47. {
  48.     int $i ;
  49.     float $x[] ;
  50.     float $angle = 360.0 / $n ;
  51.  
  52.     for( $i = 0 ; $i < $n ; $i++ ) {
  53.         float $a = $i * $angle ;    
  54.         $x[$i] =  $r * cos(deg_to_rad($a)) ;
  55.     }
  56.     return $x ;    
  57. }
  58.  
  59. proc float[] createRegularPolygonY( int $n,float $r )
  60. {
  61.     int $i ;
  62.     float $x[] ;
  63.     float $angle = 360.0 / $n ;
  64.     for( $i = 0 ; $i < $n ; $i++ ) {
  65.         float $a = $i * $angle ;    
  66.         $x[$i] =  $r * sin(deg_to_rad($a)) ;
  67.     }
  68.     return $x ;    
  69. }
  70.  
  71. proc pyramid( float $x[], float $y[], float $z[] )
  72. //
  73. //    Description :
  74. //
  75. {
  76.  
  77. }
  78.  
  79. global proc int polyIcosahedron( float $len )
  80.  
  81. {
  82.     int $i ;
  83.     int $n = 5 ;
  84.     float $zp1[5] ;
  85.     float $r = boundingCircleRadius( $n, $len ) ;
  86.  
  87.     // first pentagon.
  88.     float $xp1[] = createRegularPolygonX( $n, $r ) ;
  89.     float $yp1[] = createRegularPolygonY( $n, $r ) ;
  90.     for( $i = 0 ; $i < $n ; $i++ ) $zp1[$i] = 0.0 ;
  91.  
  92.     // second pentagon.
  93.     float $zp2[5] ;
  94.     float $xp2[] = createRegularPolygonX( $n, -1.0 * $r ) ;
  95.     float $yp2[] = createRegularPolygonY( $n, -1.0 * $r ) ;
  96.     float $dx =  ( $xp2[0] - $xp1[2] ) ;
  97.     float $dy =  ( $yp2[0] - $yp1[2] ) ;
  98.     float $rhs = $len*$len - $dx*$dx - $dy*$dy ;
  99.     float $z = sqrt(abs($rhs)); 
  100.     for( $i = 0 ; $i < $n ; $i++ ) $zp2[$i] = $z ;
  101.  
  102.  
  103.  
  104.     // create triangles.
  105.     //
  106.     string $facets[] ;
  107.     for( $i = 0 ; $i < $n ; $i++ ) {
  108.         int $k ;
  109.         int $i1 = $i+2 ;
  110.         int $i2 = $i1+1 ;
  111.         $i1 = remainder( $i1, $n ) ;    
  112.         $i2 = remainder( $i2, $n ) ;    
  113.         string $tmpFacets[] ;
  114.         $tmpFacets = `polyCreateFacet -ch 0 -p $xp1[$i1] $yp1[$i1] $zp1[$i1] -p $xp1[$i2] $yp1[$i2] $zp1[$i2] -p $xp2[$i] $yp2[$i] $zp2[$i]`;        
  115.         int $l = size($facets) ;
  116.         for( $k = 0 ; $k < size($tmpFacets) ; $k++ ) {
  117.             $facets[$l++] = $tmpFacets[$k] ;
  118.         }
  119.         int $i3 = remainder($i+1,$n) ;    
  120.         $tmpFacets = `polyCreateFacet -ch 0 -p $xp1[$i2] $yp1[$i2] $zp1[$i2] -p $xp2[$i3] $yp2[$i3] $zp2[$i3] -p $xp2[$i] $yp2[$i] $zp2[$i]`;
  121.         $l = size($facets) ;
  122.         for( $k = 0 ; $k < size($tmpFacets) ; $k++ ) {
  123.             $facets[$l++] = $tmpFacets[$k] ;
  124.         }
  125.     }
  126.  
  127.     string $ico[] ;    
  128.     // unite.
  129.     //
  130.     string $part1[] ;    
  131.     select -r $facets ;
  132.     $part1 = `polyUnite -ch 0` ;
  133.  
  134.  
  135.     // top pentagonal pyramid.
  136.     //
  137.     $z = $len*$len - ($xp1[0]*$xp1[0] + $yp1[0]*$yp1[0]) ;
  138.     $z = -1.0 * sqrt(abs($z)) ;
  139.     for( $i = 0 ; $i < $n ; $i++ ) {
  140.         int $k ;
  141.         float $x = 0.0 ;
  142.         float $y = 0.0 ;
  143.         string $tmpFacets[] ;
  144.         int $i1 = remainder($i+1,$n) ;
  145.         $tmpFacets = `polyCreateFacet -ch 0 -p $xp1[$i] $yp1[$i] $zp1[$i] -p $x $y $z -p $xp1[$i1] $yp1[$i1] $zp1[$i1]`; 
  146.         int $l = size($facets) ;
  147.         for( $k = 0 ; $k < size($tmpFacets) ; $k++ ) {
  148.             $facets[$l++] = $tmpFacets[$k] ;
  149.         }
  150.     }
  151.     string $part2[] ;    
  152.     select -r $facets ;
  153.     $part2 = `polyUnite -ch 0` ;
  154.  
  155.     // bottom pentagonal pyramid.
  156.     //
  157.     float $zt = $len*$len - ($xp2[0]*$xp2[0] + $yp2[0]*$yp2[0] ) ;
  158.     $z = sqrt(abs($zt)) + $zp2[0] ;
  159.     for( $i = 0 ; $i < $n ; $i++ ) {
  160.         int $k ;
  161.         float $x = 0.0 ;
  162.         float $y = 0.0 ;
  163.         string $tmpFacets[] ;
  164.         int $i1 = remainder($i+1,$n) ;
  165.         $tmpFacets = `polyCreateFacet -ch 0 -p $x $y $z -p $xp2[$i] $yp2[$i] $zp2[$i] -p $xp2[$i1] $yp2[$i1] $zp2[$i1]`; 
  166.         int $l = size($facets) ;
  167.         for( $k = 0 ; $k < size($tmpFacets) ; $k++ ) {
  168.             $facets[$l++] = $tmpFacets[$k] ;
  169.         }
  170.     }
  171.  
  172.     // atlast the polyhedron.
  173.     //
  174.     string $part3[] ;    
  175.     select -r $facets ;
  176.     $part3 = `polyUnite -ch 0` ;
  177.     string $ico[] = `polyUnite -ch 0 $part1[0] $part2[0] $part3[0]` ;
  178.     
  179.     select -r $ico[0] ; 
  180.         
  181.     return 0 ;
  182. }
  183.  
  184.  
  185.